using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

// TODO: replace this with the type you want to import.
using TImport = Scurvy.Media.Pipeline.DxtContent;

using Drawing = System.Drawing;
using Imaging = System.Drawing.Imaging;

using Scurvy.Media.Pipeline.Video;
using System.Runtime.InteropServices;

namespace Scurvy.Media.Pipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to import a file from disk into the specified type, TImport.
    /// 
    /// This should be part of a Content Pipeline Extension Library project.
    /// 
    /// TODO: change the ContentImporter attribute to specify the correct file
    /// extension, display name, and default processor for this importer.
    /// </summary>
    [ContentImporter(".png", DisplayName = "DXT Importer - Scurvy", DefaultProcessor = "PassThroughProcessor")]
    public class DxtImporter : ContentImporter<TImport>
    {
        private byte[] pixelBuffer;

        public override TImport Import(string filename, ContentImporterContext context)
        {
            TImport content = new TImport();

            Drawing.Bitmap bmp = new System.Drawing.Bitmap(filename);
            Drawing.Graphics g = Drawing.Graphics.FromImage(bmp);

            PlatformSurface surface = PlatformSurface.Create();
            GetData(bmp, surface);

            BitmapContent bitmapContent = surface.FromGdiBitmap(bmp);
            surface.SetData(bitmapContent, pixelBuffer);
            Texture2DContent c = new Texture2DContent();
            c.Faces[0].Add(bitmapContent);

            c.ConvertBitmapType(typeof(Dxt1BitmapContent));
            
            content.Data = c.Faces[0][0].GetPixelData();

            Dxt1BitmapContent dcontent = new Dxt1BitmapContent(bmp.Width, bmp.Height);
            dcontent.SetPixelData(content.Data);

            content.Data = dcontent.GetPixelData();

            content.Size = new Vector2(bmp.Width, bmp.Height);
            return content;
        }

        private void GetData(Drawing.Bitmap bmp, PlatformSurface surface)
        {
            // Lock the bitmap's bits.
            Drawing.Rectangle rect = new Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
            Imaging.BitmapData bmpData =
            bmp.LockBits(rect, Imaging.ImageLockMode.ReadOnly,
                         bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int numBytes = bmp.Width * bmp.Height * surface.SurfaceMultiplier;
            if (pixelBuffer == null)
            {
                pixelBuffer = new byte[numBytes];
            }

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, pixelBuffer, 0, numBytes);
            bmp.UnlockBits(bmpData);
        }

    }
}
